package com.yzcrm.erp.wiki.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.oa.service.OaBackLogService;
import com.yzcrm.erp.wiki.service.WikiCareerAssessmentService;
import com.yzcrm.utils.BaseUtil;
import com.yzcrm.utils.R;

/**
 * @author lg
 */
public class WikiCareerAssessmentController extends Controller {
    @Inject
    WikiCareerAssessmentService wikiCareerAssessmentService;

    /**
     * 	@author lg
     *	检查用户是否已经填写过
     */
	@Clear
    public void checkUser(@Para("type")Integer type, @Para("username")String username,@Para("phone")String phone){
		if(type == 1) {
//			查看报告
			Record record = Db.findFirst("select * from yzcrm_wiki_career_assessment where phone = ? ", phone);
			String errMsg = "";
			if(record != null) {
				if(!username.equals(record.getStr("username"))) {
					renderJson(R.error("姓名和手机号信息不匹配"));
				}else {
					renderJson(R.ok());
				}
				
			}else {
				renderJson(R.error("该用户未填写过问卷"));
			}
		}
		if(type == 2) {
//			填写问卷
			Record record = Db.findFirst("select * from yzcrm_wiki_career_assessment where phone=?", phone);
	    	renderJson(R.isSuccess(record == null,"该手机号已提交过问卷"));
		}
    	
    }
	
	/**
     * 	@author lg
     *	获取题目
     */
	@Clear
	public void getCareerAssessmentSubjects(){
    	renderJson(wikiCareerAssessmentService.getCareerAssessmentSubjects());
    }
	
	/**
     * 	@author lg
     *	提交问卷
     */
	@Clear
	public void setCareerAssessmentSubjects(){
		JSONObject jsonObject = JSON.parseObject(getRawData());
    	renderJson(wikiCareerAssessmentService.setCareerAssessmentSubjects(jsonObject));
    }
	
	/**
     * 	@author lg
     *	获取个人分析报告
     */
	@Clear
	public void getAnalysisReport(@Para("phone")String phone){
    	renderJson(wikiCareerAssessmentService.getAnalysisReport(phone));
    }
	
	/**
     * 	@author lg
     *	获取纬度信息
     */
	public void getDimensionConfig() {
		List<Record> records = Db.find("select * from yzcrm_admin_config where modular_type = 'careerAssessment'");
		JSONObject json = new JSONObject();
		json.put("dimensionConfig", records);
		renderJson(R.ok().put("data", json));
	}
	
	/**
     * 	@author lg
     *	获取个人分析报告
     */
	public void getCareerAssessmentList(BasePageRequest basePageRequest){
		renderJson(wikiCareerAssessmentService.getCareerAssessmentList(basePageRequest));
    }
}
